/*
package com.xiaodeng.xdblogspringboot.task;

import com.xiaodeng.xdblogspringboot.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.xiaodeng.xdblogspringboot.constant.RedisConstants.VIEW_COUNT_KEY;

@Component
public class ScheduledTask {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ArticleMapper articleMapper;
    @Scheduled(fixedRate = 1000 * 60 * 60) // 每小时执行一次
    public void syncViewCounts() {
        // 获取所有的 articleIds，假设你有一个方法可以获取所有的 articleId
        List<Long> articleIds = articleMapper.findAllarticlId();
        for (Long articleId : articleIds) {
            String key = VIEW_COUNT_KEY + articleId;
            String viewCountStr = stringRedisTemplate.opsForValue().get(key);
            if (viewCountStr != null) {
                Integer viewCount = Integer.parseInt(viewCountStr);
                // 将浏览量写入数据库
                articleMapper.updateViewCount(articleId, viewCount);
            }
        }
    }
}
*/
